<?php

namespace handle\message\lib\manage;

use Exception;
use handle\message\lib\manage\lib\ChessBoard;
use handle\message\lib\traits\ManageObserver;

class ChessBoardManage
{
    use ManageObserver;

    /**
     * 创建棋盘
     * @param string $room_id       房间ID编号
     * @return ChessBoard
     */
    public function create(string $room_id): ChessBoard
    {
        if(!isset($this->observer[$room_id]) or empty($this->observer[$room_id])){
            $this->observer[$room_id] = new ChessBoard($room_id);
            $this->observer[$room_id]->setManage(self::$instance);
            $this->observer[$room_id]->init();
        }
        return $this->observer[$room_id];
    }

    /**
     * 更新棋盘
     * @param string $room_id 房间ID编号
     * @param string $x
     * @param string $y
     * @param int $chessColor 棋子颜色
     * @param int $uid
     * @return void
     * @throws Exception
     */
    public function updateBoard(string $room_id, string $x, string $y, int $chessColor, int $uid)
    {
        $this->get($room_id)->update($x, $y, $chessColor, $uid);
    }

    /**
     * 判断某个位置上是否已经有棋子
     * @param string $room_id
     * @param string $x
     * @param string $y
     * @return bool
     * @throws Exception
     */
    public function checkSomeXY(string $room_id, string $x, string $y): bool
    {
        return $this->get($room_id)->checkSomeXY($x, $y);
    }

    /**
     * 悔棋
     * @param string $room_id
     * @return void
     * @throws Exception
     */
    public function withdraw(string $room_id)
    {
        $this->get($room_id)->withdraw();
    }

    /**
     * 获取当前棋局的回合数
     * @param string $room_id
     * @return int
     * @throws Exception
     */
    public function getRound(string $room_id): int
    {
        return $this->get($room_id)->getRound();
    }

}